// 从列表中选取两数相加，和为 target， 返回索引列表。[1, 2, 3, 4, 5, 5, 7, 8, 9, 10, 11, 12]   target 8
console.log(process.pid)
function findIndex(arr: Array<number>, target: number): Number | Array<number> {
  for (let i = 0; i < arr.length; i++) {
    let current = arr[i]
    for (let j = 0; j < arr.length; j++) {
      let next = arr[j]
      if ((current + next) == target) {
        return [i, j]
      }
    }
  }
  return -1
}

function findIndex1(arr: Array<number>, target: number): Number | Array<number> {
  let cache = new Map()
  for (let i = 0; i < arr.length; i++) {
    cache.set(arr[i], i)
  }
  for (let i = 0; i < arr.length; i++) {
    let other = target - arr[i]
    let otherIndex = cache.get(other)
    if (otherIndex === undefined) {
      continue
    }
    return [i, otherIndex]
  }
  return -1
}

let arr = new Array(1000000).fill(1).concat([1,2,3,4,5,6,7,8,9])
let target = 13
console.time("a")
console.log(findIndex(arr, target)) // O(n^2)
// console.log(findIndex1(arr, target)) // O(n)
console.timeEnd("a")

while(1){}